|
Copyright (C) 2003,2004 dr. Cristiano Sadun | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.sadun.text.ffp.LineFormat
An ojbect of this class holds details of a specific logical line format and
can parse it from core memory.
A logical line can be composed by several consecutive physical lines,
separated by a line separator
character sequence.
A LineFormat is built by declaring the expected fields and used by invoking the
parse(String)
method.
Three ways are available to declare the expected fields:
defineField()
overloads.
The overload with most parameters allows to specify the physical line containing the field,
a name for the field, a type
, its zero-based start and end indexes (end excluded)
and a possible image - a denotation that must be matched by the field value once parsed.
Type | Image |
Type.CONSTANT | required, the constant value |
Type.ALFA | optional, null or a regular expression which must be matched by the value |
Type.NUMERIC | optional, null or a NumberFormat string which will be used to parse the value |
Type.UNDEFINED | ignored |
For example,
format.defineField(0, 5, Type.NUMERIC); format.defineField(5, 7, Type.CONSTANT, "aa"); format.defineField(7, 12, Type.NUMERIC);
defineNextField()
overloads.
defineNextField(int, int, Type, String)
behaves exactly as
defineField(int, int, int, Type, String)
, but the start index is implicit:
the LineFormat keeps track a "current start" index which is updated with each definition.
For example,
format.defineNextField(5, Type.NUMERIC); format.defineNextField(7, Type.CONSTANT, "aa"); format.defineNextField(12, Type.NUMERIC);
declareLineImage()
overload.
Line images use sequences of characters (separated by spaces) to denote types:
Symbol | Type |
@ | Type.ALFA |
# | Type.NUMERIC |
any character | Type.CONSTANT |
/b | blank in Type.CONSTANT |
// | forward slash in Type.CONSTANT |
For example,
format.declareLineImage("##### aa #####");
For each method, overloads which accept a physical line number as parameter or not are provided: if the parameter is absent, the physical line number is intended 1 (the common case of parsing a single line).
A LineFormat can be copied
in whichever moment, and the resulting copy
is independent from the original object. This allows to minimize the amount of declarations
necessary for creating LineFormats for similar lines.
When parse(String)
is invoked, the object attempts to parse the passed text
according to the format definition. If the parsing fails, a FFPParseException
subexception is thrown. If the line is parsed correctly but some characters are still existing before
the line separator:
isFailOnTrailingChars()
is true a TrailingCharactersException
is thrown;
FlatFileParser.LOGGER_CHANNEL_NAME
channel.
isFailOnTrailingChars()
is initialized on construction depending on the value
of the public static field defaultFailOnTrailingChars
, which is by default set to true (i.e.
indicating that an exception should be raised on trailing characters).
Nested Class Summary | |
class |
LineFormat.FieldInfo
A class describing a field. |
Field Summary | |
static boolean |
defaultFailOnTrailingChars
This member controls the default value of the failOnTrailingChars property. |
Constructor Summary | |
LineFormat()
Create a LineFormat, for reading a logical line. |
|
LineFormat(java.lang.String name)
Create a LineFormat with a name, for reading a logical line. |
Method Summary | |
LineFormat |
createCopy()
Create an independent copy of the format. |
void |
declareLineImage(int physicalLine,
java.lang.String image)
This method allows to declare quickly a line format by providing an image. |
void |
declareLineImage(java.lang.String image)
This method allows to declare quickly a line format by providing an image. |
void |
defineField(int start,
int end)
Define a field as being at the given start-end position. |
void |
defineField(int physicalLine,
int start,
int end)
Define a field as being in the given physical line and at the given start-end position. |
void |
defineField(int physicalLine,
int start,
int end,
Type type)
Define a field as being in the given physical line and at the given start-end position. |
void |
defineField(int physicalLine,
int start,
int end,
Type type,
java.lang.String image)
Define a field as being in the given physical line and at the given start-end position. |
void |
defineField(int start,
int end,
Type type)
Define a field as being at the given start-end position and having the given type. |
void |
defineField(int physicalLine,
java.lang.String name,
int start,
int end)
Define a field as being in the given physical line and at the given start-end position. |
void |
defineField(int physicalLine,
java.lang.String name,
int start,
int end,
Type type)
Define a field as being in the given physical line and at the given start-end position. |
void |
defineField(int physicalLine,
java.lang.String name,
int start,
int end,
Type type,
java.lang.String image)
Define a field as being in the given physical line and at the given start-end position. |
void |
defineField(java.lang.String name,
int start,
int end)
Define a field as being at the given start-end position and having the given name. |
void |
defineField(java.lang.String name,
int start,
int end,
Type type)
Define a field as being at the given start-end position and having the given name and type. |
void |
defineFields(int[][] positions)
Define fields using a positions array. |
void |
defineFields(int physicalLine,
int[][] positions)
Define fields in the given physical line using a positions array. |
void |
defineNextField(int end)
Define a field as being located from the current start position to the given end position (excluded). |
void |
defineNextField(int physicalLine,
int end)
Define a field as being in the given physical line, from the current start position to the given end position (excluded). |
void |
defineNextField(int physicalLine,
int end,
Type type)
Define a field as being in the given physical line, having the given type and being located from the current start position to the given end position (excluded). |
void |
defineNextField(int physicalLine,
int end,
Type type,
java.lang.String image)
Define a field as being in the given physical line, having the given type and image and being located from the current start position to the given end position (excluded). |
void |
defineNextField(int physicalLine,
java.lang.String name,
int end)
Define a field as being in the given physical line, having the given name and being located from the current start position to the given end position (excluded). |
void |
defineNextField(int physicalLine,
java.lang.String name,
int end,
Type type)
Define a field as being in the given physical line, having the given name and type and being located from the current start position to the given end position (excluded). |
void |
defineNextField(int physicalLine,
java.lang.String name,
int end,
Type type,
java.lang.String image)
Define a field as being in the given physical line, having the given name, type and image and being located from the current start position to the given end position (excluded). |
void |
defineNextField(int end,
Type type)
Define a field as having the given type and being located from the current start position to the given end position (excluded). |
void |
defineNextField(int end,
Type type,
java.lang.String image)
Define a field as having the given type and image and being located from the current start position to the given end position (excluded). |
void |
defineNextField(java.lang.String name,
int end)
Define a field as having the given name and being located from the current start position to the given end position (excluded). |
void |
defineNextField(java.lang.String name,
int end,
Type type)
Define a field as having the given name and being located from the current start position to the given end position (excluded), and given type |
void |
defineNextField(java.lang.String name,
int end,
Type type,
java.lang.String image)
Define a field as having the given name and being located from the current start position to the given end position (excluded), and given type and image |
void |
defineNextFields(int[] positions)
Define consecutive fields using a positions array. |
void |
defineNextFields(int physicalLine,
int[] positions)
Define consecutive fields in the given physical line using a positions array. |
static java.lang.String |
extract(java.lang.CharSequence line,
int start,
int end)
An utility method to extracts a portion of a physical line. |
java.lang.String |
formatValuesAsXML(int lineCount,
java.lang.String[] values)
An utility method to format a set of values to an XML format, according to this LineFormat . |
java.lang.String |
formatValuesAsXML(java.lang.String[] values)
An utility method to format a set of values to an XML format, according to this LineFormat . |
static LineFormat |
fromImage(java.lang.String image)
Allows to create a LineFormat object directly from an image. |
static LineFormat |
fromImage(java.lang.String name,
java.lang.String image)
Allows to create a named LineFormat object directly from an image. |
int |
getFieldsCount()
Return the number of fields in the logical line. |
int |
getFieldsCount(int physicalLine)
Return the number of fields in the physical line. |
java.lang.String |
getLineSeparator()
Return the line separator. |
java.lang.String |
getName()
Return the name of this line format. |
int |
getPhysicalLinesCount()
Return the physical lines count at the moment of call. |
boolean |
isFailOnTrailingChars()
Return whether or not parsing must fails if a line is parsed correctly but not all its characters are consumed. |
java.util.Iterator |
iterator()
Return an iterator on this format's LineFormat.FieldInfo set. |
static java.lang.String |
makeConstantImage(java.lang.String s)
Return a string constant translated to the image syntax recognized by this class. |
java.lang.String[] |
parse(java.lang.String s)
Parse the given line basing on the format. |
java.lang.String[] |
parse(java.lang.String s,
boolean autoTrim)
Parse the given line basing on the format. |
java.lang.String |
parseToXml(java.lang.String s)
An utility method, which parses a line, and returns its XML fragment representation. |
java.lang.String |
parseToXml(java.lang.String s,
boolean autoTrim)
An utility method, which parses a line, and returns its XML fragment representation. |
void |
setFailOnTrailingChars(boolean failOnTrailingChars)
Set whether or not parsing must fails if a line is parsed correctly but not all its characters are consumed. |
void |
setLineSeparator(java.lang.String lineSeparator)
Set the line separator. |
java.lang.String |
toString()
Return a human-readable description of the format. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static boolean defaultFailOnTrailingChars
failOnTrailingChars
property. It is set to true when the class is
loaded.
Constructor Detail |
public LineFormat(java.lang.String name)
A logical line may be composed by multiple physical lines separated by a
line separator
.
public LineFormat()
A logical line may be composed by multiple physical lines separated by a
line separator
.
Method Detail |
public java.lang.String getName()
public java.lang.String getLineSeparator()
public void setLineSeparator(java.lang.String lineSeparator)
lineSeparator
- The lineSeparator to set.public int getPhysicalLinesCount()
Note that if defineField(int, int, int)
is invoked declaring a
field into a greater physical line, the count returned by this method
will adjust accordingly.
public static java.lang.String extract(java.lang.CharSequence line, int start, int end) throws IllegalRangeException
line
- the character array containing the physical linestart
- the start indexend
- the end index
IllegalRangeException
- if the given range is not validpublic void defineFields(int physicalLine, int[][] positions)
positions
- as many fields as elements in the array are
defined. Each element of the array must be a two-integer array
containing start and end position of the field (excluded).public void defineFields(int[][] positions)
positions
- as many fields as elements in the array are
defined. Each element of the array must be a two-integer array
containing start and end position of the field (excluded).public void defineField(int physicalLine, int start, int end)
Physical lines indexes start at 1. Start and end indexes start at 0.
Note: if the given physical line is greater than the current physical
line, the latter is increased; the current start position is advanced to
the given end position (for usage with
defineNextField(int, int)
).
physicalLine
- the physical line index (starting from 1)start
- the start index in the lineend
- the end index in the linepublic void defineField(int physicalLine, int start, int end, Type type)
Physical lines indexes start at 1. Start and end indexes start at 0.
Note: if the given physical line is greater than the current physical
line, the latter is increased; the current start position is advanced to
the given end position (for usage with
defineNextField(int, int)
).
physicalLine
- the physical line index (starting from 1)start
- the start index in the lineend
- the end index in the linetype
- the expected type of the parameterpublic void defineField(int physicalLine, int start, int end, Type type, java.lang.String image)
Physical lines indexes start at 1. Start and end indexes start at 0.
Note: if the given physical line is greater than the current physical
line, the latter is increased; the current start position is advanced to
the given end position (for usage with
defineNextField(int, int)
).
physicalLine
- the physical line index (starting from 1)start
- the start index in the lineend
- the end index in the linetype
- the expected type of the parameterimage
- the image of the elementpublic void defineField(int physicalLine, java.lang.String name, int start, int end)
Physical lines indexes start at 1. Start and end indexes start at 0.
Note: if the given physical line is greater than the current physical
line, the latter is increased; the current start position is advanced to
the given end position (for usage with
defineNextField(int, int)
).
physicalLine
- the physical line index (starting from 1)name
- the name of the fieldstart
- the start index in the lineend
- the end index in the linepublic void defineField(int physicalLine, java.lang.String name, int start, int end, Type type)
Physical lines indexes start at 1. Start and end indexes start at 0.
Note: if the given physical line is greater than the current physical
line, the latter is increased; the current start position is advanced to
the given end position (for usage with
defineNextField(int, int)
).
physicalLine
- the physical line index (starting from 1)name
- the name of the fieldstart
- the start index in the lineend
- the end index in the linetype
- the expected type of the parameterpublic void defineField(int physicalLine, java.lang.String name, int start, int end, Type type, java.lang.String image)
Physical lines indexes start at 1. Start and end indexes start at 0.
Note: if the given physical line is greater than the current physical
line, the latter is increased; the current start position is advanced to
the given end position (for usage with
defineNextField(int, int)
).
This is the method allowing the most complete field definition.
physicalLine
- the physical line index (starting from 1)name
- the name of the fieldstart
- the start index in the lineend
- the end index in the linetype
- the expected type of the parameterimage
- the image of the elementpublic void defineField(int start, int end)
Note: the current start position is advanced to the given end position
(for usage with defineNextField(int, int)
).
start
- the start index in the lineend
- the end index in the linepublic void defineField(int start, int end, Type type)
Note: the current start position is advanced to the given end position
(for usage with defineNextField(int, int)
).
start
- the start index in the lineend
- the end index in the linetype
- the expected type of the parameterpublic void defineField(java.lang.String name, int start, int end)
Note: the current start position is advanced to the given end position
(for usage with defineNextField(int, int)
).
name
- the name of the fieldstart
- the start index in the lineend
- the end index in the linepublic void defineField(java.lang.String name, int start, int end, Type type)
Note: the current start position is advanced to the given end position
(for usage with defineNextField(int, int)
).
name
- the name of the fieldstart
- the start index in the lineend
- the end index in the linetype
- the expected type of the parameterpublic void defineNextFields(int physicalLine, int[] positions)
positions
- as many fields as elements in the array are
defined. Each element indicates the end
position of the field (excluded).public void defineNextFields(int[] positions)
positions
- as many fields as elements in the array are
defined. Each element indicates the end
position of the field (excluded).public void defineNextField(int physicalLine, int end)
physicalLine
- the physical line index (starting from 1)end
- the end position (excluded).public void defineNextField(int physicalLine, int end, Type type)
physicalLine
- the physical line index (starting from 1)end
- the end position (excluded).type
- the expected type of the parameterpublic void defineNextField(int physicalLine, int end, Type type, java.lang.String image)
physicalLine
- the physical line index (starting from 1)end
- the end position (excluded).type
- the expected type of the parameterimage
- the image of the parameterpublic void defineNextField(int physicalLine, java.lang.String name, int end)
name
- the name of the fieldphysicalLine
- the physical line index (starting from 1)end
- the end position (excluded).public void defineNextField(int physicalLine, java.lang.String name, int end, Type type)
name
- the name of the fieldphysicalLine
- the physical line index (starting from 1)end
- the end position (excluded).type
- the expected type of the parameterpublic void defineNextField(int physicalLine, java.lang.String name, int end, Type type, java.lang.String image)
name
- the name of the fieldphysicalLine
- the physical line index (starting from 1)end
- the end position (excluded).type
- the expected type of the parameterimage
- the image of the parameterpublic void defineNextField(int end)
end
- the end position (excluded).public void defineNextField(java.lang.String name, int end)
name
- the name of the fieldend
- the end position (excluded).public void defineNextField(java.lang.String name, int end, Type type)
name
- the name of the fieldend
- the end position (excluded)type
- the expected type of the parameterpublic void defineNextField(java.lang.String name, int end, Type type, java.lang.String image)
name
- the name of the fieldend
- the end position (excluded).type
- the expected type of the parameterimage
- the image of the parameterpublic void defineNextField(int end, Type type)
end
- the end position (excluded).*type
- the expected type of the parameterpublic void defineNextField(int end, Type type, java.lang.String image)
end
- the end position (excluded).*type
- the expected type of the parameterimage
- the image of the parameterpublic LineFormat createCopy()
The copy so obtained can be modifed independetly from the original, for example to parse two slightly different formats.
public java.lang.String[] parse(java.lang.String s) throws FFPParseException
isFailOnTrailingChars()
is true,
the match must be total. Else, trailing characters are silently ignored.
s
- a string containing the line's physical lines. The last line
may or may not have a
line separator
.
FFPParseException
- if a problem occurs when parsingpublic java.lang.String[] parse(java.lang.String s, boolean autoTrim) throws FFPParseException
isFailOnTrailingChars()
is true,
the match must be total. Else, trailing characters are silently ignored.
Basing on the value of the autoTrim parameter, values are automatically trimmed before being returned.
s
- a string containing the line's physical lines. The last line
may or may not have a
line separator
.autoTrim
- if true strings are trimmed before being returned
FFPParseException
- if a problem occurs when parsingpublic java.lang.String parseToXml(java.lang.String s) throws FFPParseException
s
- a string containing the line's physical lines. The last line
may or may not have a
line separator
.
FFPParseException
- if a problem occurs when parsingpublic java.lang.String parseToXml(java.lang.String s, boolean autoTrim) throws FFPParseException
s
- a string containing the line's physical lines. The last line
may or may not have a
line separator
.autoTrim
- if true strings are trimmed before being returned
FFPParseException
- if a problem occurs when parsingpublic java.lang.String formatValuesAsXML(int lineCount, java.lang.String[] values)
LineFormat
.
values
- the values to format to XML
public java.lang.String formatValuesAsXML(java.lang.String[] values)
LineFormat
.
values
- the values to format to XML
public java.lang.String toString()
public void declareLineImage(java.lang.String image) throws ImageParseException
The image symbols indicate the type of the field; fields are separated by a space.
image
-
ImageParseException
public void declareLineImage(int physicalLine, java.lang.String image) throws ImageParseException
The image symbols indicate the type of the field; fields are separated by a space.
physicalLine
- image
-
ImageParseException
public static LineFormat fromImage(java.lang.String image) throws ImageParseException
declareLineImage(String)
).
image
- the format image
ImageParseException
- if the image syntax is incorrect.public static LineFormat fromImage(java.lang.String name, java.lang.String image) throws ImageParseException
declareLineImage(String)
).
image
- the format image
ImageParseException
- if the image syntax is incorrect.public boolean isFailOnTrailingChars()
public void setFailOnTrailingChars(boolean failOnTrailingChars)
The initial value is controlled by the defaultFailOnTrailingChars
static member.
failOnTrailingChars
- true if a line which parsed correctly, but
whose characters are not totally consumed must make the parser fail.public int getFieldsCount()
public int getFieldsCount(int physicalLine)
public java.util.Iterator iterator()
LineFormat.FieldInfo
set.
LineFormat.FieldInfo
set.public static java.lang.String makeConstantImage(java.lang.String s)
|
Copyright (C) 2003,2004 dr. Cristiano Sadun | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |